#include "options.h"
#include <QStringList>
#include <QObject>

Options * Options::globalOptions = new Options(true);

Options * Options::getGlobalOptions(){
    return globalOptions;
}

Options::Options(bool useDefault){
    useGlobalOptions = true;
    if (useDefault){
	baseOptions.insert("isFavoriteMode", true);
	
	baseOptions.insert("isAutoLogin", true);
	baseOptions.insert("isGuestMode", false);
	baseOptions.insert("isSaveUsername", true);
	baseOptions.insert("isSavePassword", true);
	
	baseOptions.insert("isUseDock", true);
	baseOptions.insert("isStayOnSystray", true);
	//global options
	baseOptions.insert("isAlarm", false);
	baseOptions.insert("isAlarmBoard", true);
	baseOptions.insert("isAlarmTopic", true);
	baseOptions.insert("isAlarmMessage", true);
	//use by Profile
	baseOptions.insert("isAlarmAny", false);
	baseOptions.insert("isAlarmArticle", true);
	baseOptions.insert("isAlarmFriend", false);
	baseOptions.insert("isAlarmLoveBoard", true);
	baseOptions.insert("isAlarmLoveTopic", true);
	
	baseOptions.insert("isAutoCheckNewBoard", true);
	baseOptions.insert("isAutoCheckNewTopic", true);
	baseOptions.insert("isAutoCheckNewArticle", true);

	baseOptions.insert("isDoubleClickMode", false);
    }
    else
	copy(*globalOptions);
}

void Options::copy(Options & other){
    baseOptions = other.baseOptions;
    extendOptions = other.extendOptions;
}

bool Options::isUseGlobalOptions() const{
    return useGlobalOptions;
}

bool Options::setUseGlobalOptions(bool value){
    return useGlobalOptions = value;
}

bool Options::getOption(QString optionName) const{
    /*if (useGlobalOptions)
	return globalOptions->baseOptions.value(optionName, false);
    else*/
	return baseOptions.value(optionName, false);
}

//保存至基本选项
void Options::setOption(QString optionName, bool value){
    /*if (useGlobalOptions)	//此时修改全局选项
	globalOptions->baseOptions.insert(optionName, value);
    else*/
	baseOptions.insert(optionName, value);
}

//保存至附加选项
void Options::setExtendOption(QString optionName, QVariant value){
    /*if (useGlobalOptions)	//此时修改全局选项
	globalOptions->extendOptions.insert(optionName, value);
    else*/
	extendOptions.insert(optionName, value);
}

QVariant Options::getExtendOption(QString optionName){
    /*if (useGlobalOptions)
	return globalOptions->extendOptions.value(optionName, QObject::tr("unset"));
    else*/
	return extendOptions.value(optionName, QObject::tr("unset"));
}

void Options::restore(QSettings& settings){
    DEBUG_F();
    QStringList names;
    settings.beginGroup("baseOptions");
    names = settings.allKeys();
    foreach(QString name, names)
	baseOptions.insert(name, settings.value(name).toBool());
    settings.endGroup();
    settings.beginGroup("extendOptions");
    names = settings.allKeys();
    foreach(QString name, names)
	baseOptions.insert(name, settings.value(name).toBool());
    settings.endGroup();
}

void Options::save(QSettings& settings){
    DEBUG_F();
    QStringList names;
    settings.beginGroup("baseOptions");
    names = baseOptions.keys();
    foreach(QString name, names)
	settings.setValue(name, baseOptions.value(name));
    settings.endGroup();
    settings.beginGroup("extendOptions");
    names = extendOptions.keys();
    foreach(QString name, names)
	settings.setValue(name, extendOptions.value(name));
    settings.endGroup();
}

void Options::erase(QSettings& settings){
    settings.remove("baseOptions");
    settings.remove("extendOptions");
}
